package org.easy.db;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.sql.DataSource;

/**
 * @Description: 数据库基础信息
 * @Author: wxl
 * @CreateTime: 2022/10/27 17:40
 */
@Data
@ApiModel(value = "数据库基本信息实体类",description = "数据库基本信息实体类")
public class DataBaseInfo {

    public DataBaseInfo () {}

    @ApiModelProperty(name = "url",value = "连接地址")
    private String url;

    @ApiModelProperty(name = "port",value = "端口")
    private int port;

    @ApiModelProperty(name = "type",value = "数据库类型")
    private Type type;

    @ApiModelProperty(name = "userName",value = "用户名")
    private String userName;

    @ApiModelProperty(name = "password",value = "密码")
    private String password;

    @ApiModelProperty(name = "dataBase",value = "数据库名称")
    private String dataBase;

    @ApiModelProperty(name = "driver",value = "驱动",hidden = true)
    private String driver;

    @ApiModelProperty(name = "dataSource",value = "数据库",hidden = true)
    private DataSource dataSource;

    public void initDataSource() {
        this.type = Type.getType(this.type.name());
        this.url = this.type.getUrlByType(url,port,dataBase);
        this.driver = this.type.getDriver();
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setDriverClassName(this.driver);
        hikariConfig.setJdbcUrl(this.url);
        hikariConfig.setUsername(this.userName);
        hikariConfig.setPassword(this.password);
        hikariConfig.setAutoCommit(true);
        hikariConfig.setConnectionTestQuery("SELECT 1");
        hikariConfig.setConnectionTimeout(30000);
        hikariConfig.setIdleTimeout(30000);
        hikariConfig.setMaxLifetime(900000);
        hikariConfig.setMaximumPoolSize(15);
        hikariConfig.setMinimumIdle(10);
        hikariConfig.setPoolName("HikariCP");
        this.dataSource =  new HikariDataSource(hikariConfig);
    }

}
